﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using One.Models;
using OneWeb.DbContexts;
using OneWeb.ViewModels;
using PagedList;

namespace OneWeb.Controllers
{
    [RoutePrefix("answer")]
    public class AnswerController : Controller
    {
        private OneDataContext db = new OneDataContext();

        private int pageSize = 10;

        // GET: Answers
        public async Task<ActionResult> Index(int? page = 1)
        {
            AnswerListPageViewModel viewModel = new AnswerListPageViewModel();
            viewModel.Answers = db.Answers.ToList().ToPagedList(page ?? 1, pageSize);
            return View(viewModel);
        }

        // GET: Answers/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Answer answer = await db.Answers.FindAsync(id);
            if (answer == null)
            {
                return HttpNotFound();
            }
            return View(answer);
        }

        //// GET: Answers/Create
        //public ActionResult Create()
        //{
        //    return View();
        //}

        //// POST: Answers/Create
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> Create([Bind(Include = "Id,QuestionTitle,AnswerTitle,AnswerContent,MakeTime,AskerName,AskerAvatar,AuthorName,AuthorAvatar")] Answer answer)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Answers.Add(answer);
        //        await db.SaveChangesAsync();
        //        return RedirectToAction("Index");
        //    }

        //    return View(answer);
        //}

        //// GET: Answers/Edit/5
        //public async Task<ActionResult> Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Answer answer = await db.Answers.FindAsync(id);
        //    if (answer == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(answer);
        //}

        //// POST: Answers/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> Edit([Bind(Include = "Id,QuestionTitle,AnswerTitle,AnswerContent,MakeTime,AskerName,AskerAvatar,AuthorName,AuthorAvatar")] Answer answer)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(answer).State = EntityState.Modified;
        //        await db.SaveChangesAsync();
        //        return RedirectToAction("Index");
        //    }
        //    return View(answer);
        //}

        //// GET: Answers/Delete/5
        //public async Task<ActionResult> Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Answer answer = await db.Answers.FindAsync(id);
        //    if (answer == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(answer);
        //}

        //// POST: Answers/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> DeleteConfirmed(int id)
        //{
        //    Answer answer = await db.Answers.FindAsync(id);
        //    db.Answers.Remove(answer);
        //    await db.SaveChangesAsync();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
